#include "bsp_am1008.h"
#include "bsp_gpioband.h"
#include "usart.h"
#include <string.h>

extern UART_HandleTypeDef huart3;
static uint8_t amCMD[] = {0x11, 0x01, 0x16, 0xD8};
static uint8_t amRcv[100] = { 0 };
uint8_t amGetPM25AndVoc(uint32_t * pm2d5, uint32_t * pm1d0, uint32_t * pm10, uint32_t * voc)
{
	HAL_UART_Transmit(&huart3, amCMD, sizeof(amCMD), 0xFFFF);
	memset(amRcv, 0x00, sizeof(amRcv));
	HAL_UART_Receive(&huart3, amRcv, 3 + 18 + 1, 512);
	if (amRcv[0] != 0x16 || amRcv[1] == 0x00)
	{
		return 1;	
	}
	u1_printhex("amrcv:", (char *)amRcv, amRcv[1] + 3);
	voc[0] = (256 * amRcv[2 + 1]) + amRcv[2 + 2];
	pm1d0[0] = (256 *  amRcv[2 + 5]) + amRcv[2 + 6];
	pm2d5[0] = (256 *  amRcv[2 + 7]) + amRcv[2 + 8];
	pm10[0] = (256 *  amRcv[2 + 9]) + amRcv[2 + 10];
	return 0;
}
	
uint8_t amRest()
{
	PAout(13) = 0;
	HAL_Delay(100);
	PAout(13) = 1;
	return 0;
}

uint8_t amWeekUpEn(uint8_t En)
{
	PAout(8) = En;
	return 0;
}

void am_example(void)
{
	uint32_t pm2d5 = 0;
	uint32_t pm10 = 0;
	uint32_t pm1d0 = 0;
	uint32_t voc = 0;
	amRest();
	amWeekUpEn(1);
	for (uint8_t i = 0; i < 10; i++)
	{
		amGetPM25AndVoc(&pm2d5,&pm1d0,&pm10 ,&voc);
		u1_printf("pm2.5:[%d],pm1.0:[%d],pm10:[%d],voc:[%d]\r\n", pm2d5, pm1d0,pm10, voc);
		HAL_Delay(1000);
	}
}
